package net.lucamasini.ricette.client.tipoingredienti;

import net.lucamasini.ricette.client.RicettarioRequestFactory;
import net.lucamasini.ricette.client.ServiceBus;

import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.Editor;
import com.google.gwt.editor.ui.client.ValueBoxEditorDecorator;
import com.google.gwt.event.dom.client.*;
import com.google.gwt.requestfactory.client.RequestFactoryEditorDriver;
import com.google.gwt.requestfactory.shared.Receiver;
import com.google.gwt.requestfactory.shared.RequestContext;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.*;

public class TipoIngredientiDialog extends SimplePanel implements Editor<TipoIngredienteProxy>{

	private final static RicettarioRequestFactory requestFactory = GWT.create(RicettarioRequestFactory.class);
	
	private static IngredientiDialogUiBinder uiBinder = GWT
			.create(IngredientiDialogUiBinder.class);

	interface IngredientiDialogUiBinder extends
			UiBinder<Widget, TipoIngredientiDialog> {
	}

	@UiField
	DialogBox dialog;
	
	@UiField
	ValueBoxEditorDecorator<String> descrizione;
		
	@UiField Button ok;
	@UiField Button annulla;

	private final TipoIngredienti ingredienti;

	private TipoIngredienteProxy ingrediente;
	
	interface TipoIngredienteDriver extends RequestFactoryEditorDriver<TipoIngredienteProxy, TipoIngredientiDialog> {}
	private TipoIngredienteDriver driver = GWT.create(TipoIngredienteDriver.class);
	
	public TipoIngredientiDialog(TipoIngredienti ingredienti) {
		add(uiBinder.createAndBindUi(this));
		
		requestFactory.initialize(ServiceBus.getEventbus());
		driver.initialize(ServiceBus.getEventbus(), requestFactory, this);
		
		this.ingredienti = ingredienti;
		
		ok.addKeyPressHandler(new KeyPressHandler() {
			@Override
			public void onKeyPress(KeyPressEvent event) {
				if (KeyCodes.KEY_ENTER == event.getNativeEvent().getKeyCode()) {
					ok.click();
				}
			}
		});
	}
	
	@UiHandler("ok")
	void onOk(ClickEvent event) {
		ok.setEnabled(false);

		final TipoIngredienteRequest context = (TipoIngredienteRequest) driver.flush();

		if (driver.hasErrors()) {
			dialog.setText("Errors detected locally");
			return;
		}

		context.salvaTipoIngrediente(ingrediente).fire(new Receiver<Void>() {
			@Override
			public void onSuccess(Void response) {
				ingredienti.refreshTipoIngredienti();
				dialog.hide();
			}
		});
	}

	@UiHandler("annulla")
	void onCancella(ClickEvent event) {
		dialog.hide();
	}

	private void setTipoIngrediente(TipoIngredienteProxy tipoIngrediente, RequestContext context) {
		
		this.ingrediente = tipoIngrediente;

		driver.initialize(this);
		driver.edit(tipoIngrediente, context);
	}

	public void creaIngrediente() {
		RequestContext context = requestFactory.tipoIngredienteRequest();
		modificaTipoIngrediente(context.create(TipoIngredienteProxy.class), context);
	}

	public void modificaTipoIngrediente(TipoIngredienteProxy i, RequestContext context) {
		setTipoIngrediente(i, context);
		
		ok.setEnabled(true);

		dialog.center();
		dialog.show();
	}
	
	public void modificaTipoIngrediente(TipoIngredienteProxy i) {
		modificaTipoIngrediente(i, requestFactory.tipoIngredienteRequest());
	}

}
